package gold.digger;

import gold.vo.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC156 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public TreeNode finalRoot = null;

        public TreeNode upsideDownBinaryTree(TreeNode root) {
            upsideDownBinaryTreeRecur(root, 1);
            return finalRoot;
        }

        // direction: 1 left branch; 2, right branch.
        public void upsideDownBinaryTreeRecur(TreeNode root, int direction) {
            if (null == root) return;
            if (null == root.left) {
                if (direction == 1) finalRoot = root;
                return;
            }

            // root 目前left 和 right都不为空
            TreeNode curLevelReturnRoot = root.left;
            upsideDownBinaryTreeRecur(root.left, 1);
            root.left.right = root;
            root.left.left = root.right;
            root.left = null;
            root.right = null;
        }
    }

    public void run() {
        Solution solution = new Solution();
        TreeNode ro = TreeNode.createTree("[1,2,3,4,5,6,7]");
        System.out.println(solution.upsideDownBinaryTree(ro));
    }

    public static void main(String[] args) throws Exception {
        LC156 an = new LC156();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
